<?php
/**
 * admin模块下的全局中间件-后置
 * Created by PhpStorm.
 * User:
 * Date: 2018/7/2
 * Time: 15:52
 */
namespace app\http\middleware;
use think\Exception;

class After{

    public function handle($request, \Closure $next)
    {
        $response = $next($request);

        // 添加中间件执行代码
        try{
            $url = request()->controller().'/'.request()->action();
            $input = [
                'uid'=>session('aid'),
                'url'=>$url,
                'ip'=>getIp(),
                'module'=>request()->module()
            ];

            $input['code'] = $response->getCode();

            $type = $response->getHeader('Content-Type');

            if(substr($type,0,4) == 'text'){
                $input['result'] = '{!DOCTYPE html}';
            }else{
                $input['result'] = $response->getContent();
            }


            db('log')->insert($input);
        }catch (Exception $exception){}


        return $response;
    }
}